package com.zhu.utils;

import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.pool.DruidPooledConnection;
import com.alibaba.fastjson.JSONObject;
import com.google.common.base.CaseFormat;
import org.apache.commons.beanutils.BeanUtils;

import java.lang.reflect.InvocationTargetException;
import java.security.acl.LastOwnerException;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

/**
 *
 * 可以适用于任何jdbc方式访问的数据库中的任何查询语句
 *
 * select count(*) from t;  单行单列
 * select * from t where id = ;  单行 多列
 * select name from t 多行 单列
 * select * from t  多行 多列
 */
public class JDBCUtil  {

    /**
     *
     * @param connection 链接对象
     * @param sql 查询语句
     * @param tClass  返回集合元素类型的class对象
     * @param <T>
     *
     * @return
     */
    public static <T> List<T> queryList(Connection connection,String sql,Class<T> tClass,Boolean underScoreToCamel) {

        List<T> resultList = new ArrayList<>();
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;

        try {
            preparedStatement = connection.prepareStatement(sql);
            resultSet = preparedStatement.executeQuery();
            ResultSetMetaData metaData = resultSet.getMetaData();  //元数据信息
          //  System.out.println(metaData.getColumnCount());
            while (resultSet.next()){

                //通过反射创建对象
                T object = tClass.newInstance();  //结果类型
                for (int i = 1; i <= metaData.getColumnCount(); i++) {
                    String columnName = metaData.getColumnName(i);
                    Object columnValue = resultSet.getObject(i);
                    // 封装名字 判断是否需要下划线和驼峰命名的转换
                    if (underScoreToCamel){
                        columnName = CaseFormat.LOWER_UNDERSCORE.to(CaseFormat.LOWER_CAMEL,columnName.toLowerCase());
                    }
                    //赋值
                    BeanUtils.setProperty(object,columnName,columnValue);
                }
                resultList.add(object);
            }
        }catch (SQLException | InstantiationException | IllegalAccessException | InvocationTargetException exception){
            exception.printStackTrace();
            throw new RuntimeException("dataQuery from database false");
        }finally {
            if(resultSet != null){
                try {
                resultSet.close();
            }catch (SQLException sqlException){
                    sqlException.printStackTrace();
                }
        }
            if(preparedStatement != null){
                try {
                    preparedStatement.close();
                }catch (SQLException sqlException){
                    sqlException.printStackTrace();
                }
            }
        }
        return resultList;
    }

    /*
    public static void main(String[] args) throws SQLException {
        DruidDataSource dataSource = ZhuDruidDataSourceUtil.createDataSource();
        DruidPooledConnection connection = dataSource.getConnection();
        String sql = "select * from GMALL_FLINK_REALTIME.DIM_BASE_TRADEMARK";
        List<JSONObject> list = queryList(connection, sql, JSONObject.class, true);
        for (JSONObject js : list) {
            System.out.println(js);
        }
        connection.close();
    }
     */

}
